package com.demo.mystudy.ui.jetinnofolder

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.demo.mystudy.R
import com.demo.mystudy.databinding.DialogJetinnofolderBinding
import com.demo.mystudy.event.DownloadFileEvent
import com.demo.mystudy.event.JetinnoFolerEvent
import com.demo.mystudy.ui.socketclient.clientcmd.ClientCmdDownloadFile
import com.demo.mystudy.ui.socketclient.clientcmd.ClientCmdJetinnoFoler
import com.jetinno.file.TextActivity
import com.jetinno.simple.SimpleApplication.Companion.sContext
import com.jetinno.simple.SimpleDialogFragment
import com.jetinno.simple.bean.FileBean
import com.jetinno.simple.utils.FragmentsUtil
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

/**
 * Created by zhangyuncai on 2025/4/28.
 * 描述：
 */
class JetinnoFolderDF : SimpleDialogFragment() {
    lateinit var mDB: DialogJetinnofolderBinding
    val filePath: String by lazy {
        requireArguments().getString("data")!!
    }
    val mDatas = arrayListOf<FileBean>()
    val mAdapter: JetinnoFolderAdapter by lazy { JetinnoFolderAdapter(mDatas) }


    override fun useDatabing(inflater: LayoutInflater, container: ViewGroup?): View? {
        mDB = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        return mDB.root
    }

    override fun getLayoutId(): Int {
        isRegisterEventBus = true
        return R.layout.dialog_jetinnofolder
    }

    override fun initView() {
        mDB.rvJetinnofolder.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = mAdapter
        }
        val file = File(filePath)
        mDB.headbar.setTitleText(file.name)
    }

    override fun initEvent() {
        mDB.headbar.setBackClickListener {
            dismiss()
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val item = mDatas[position]
            val filePath = item.filePath!!
            if (item.isFolder) {
                showSocketDF(childFragmentManager, mContext, filePath)
            } else {
            }
        }
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val item = mDatas[position]
            if (view.id == R.id.tv_jetinnofolder_download) {
                ClientCmdDownloadFile.send(item.filePath!!)
            } else if (view.id == R.id.tv_jetinnofolder_delete) {
                File(sContext.filesDir, item.filePath!!).delete()
                mAdapter.notifyDataSetChanged()
            } else if (view.id == R.id.tv_jetinnofolder_edit) {
                val file = File(sContext.filesDir, item.filePath!!)
                if (file.exists()) {
                    val intent= Intent(mContext,TextActivity::class.java)
                    intent.putExtra("data",file.absolutePath)
                    mContext.startActivity(intent)
//                    EditFileDF.showEditFileDF(childFragmentManager, mContext, file.absolutePath)
                }
            }
        }
    }

    override fun initData() {
        //请求该文件夹下的文件列表
        ClientCmdJetinnoFoler.send(filePath)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onJetinnoFolerEvent(event: JetinnoFolerEvent) {
        event.bean?.let {
            if (!it.childPathList.isNullOrEmpty()) {
                if (TextUtils.equals(it.parentPath, filePath)) {
                    mDatas.clear()
                    mDatas.addAll(it.childPathList!!)
                    mAdapter.notifyDataSetChanged()
                }
            }

        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onDownloadFileEvent(event: DownloadFileEvent) {
        mAdapter.notifyDataSetChanged()
    }

    companion object {
        /**
         * @param filePath 相对路径 例如/Jetinno
         */
        fun showSocketDF(
            fragmentManager: FragmentManager,
            context: Context,
            filePath: String
        ): JetinnoFolderDF {
            val bundle = Bundle()
            bundle.putString("data", filePath)
            return FragmentsUtil.showDialog(
                fragmentManager,
                context,
                JetinnoFolderDF::class.java,
                bundle
            )
        }
    }
}